package casa.depesos.app.com.activity;

import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import casa.depesos.app.com.R;
import casa.depesos.app.com.databinding.InfoTopBinding;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.LevelBean;
import casa.depesos.app.com.http.req.LevelReq;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;

public abstract class BaseInfoActivity extends BaseChooseActivity {

    protected boolean fail = false;

    protected abstract int getIndex();

    public abstract void getData();

    class TopTv {
        View tag;

        TopTv(View tag) {
            this.tag = tag;
        }
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        if (fromMe()) {
            disableAll(view);
        }
    }

    protected boolean fromMe() {
        return getIntent().getIntExtra("from", 0) == 1;
    }

    protected void initTop(InfoTopBinding binding) {
        binding.commonTop.back.setEnabled(true);
        binding.commonTop.back.setOnClickListener(view -> dealBack());
        binding.commonTop.title.setText("Verificación de información");
        binding.commonTop.top.setBackground(null);
        binding.top.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);

        if (getIntent().hasExtra("amount")) {
            List<String> tmp = getIntent().getStringArrayListExtra("amount");
            setAmount(binding, tmp);
        } else {
            new LevelReq().req(new ReqCallback<LevelBean>() {
                @Override
                public void callback(boolean success, LevelBean bean) {
                    if (success && bean.isValid()) {
                        getIntent().putStringArrayListExtra("amount", bean.list);
                        setAmount(binding, bean.list);
                    }
                }
            });
        }
    }

    private void setAmount(InfoTopBinding binding, List<String> tmp) {
        if (tmp.size() != 3) {
            binding.tagLinear.setVisibility(View.GONE);
            return;
        }
        binding.tagLinear.setVisibility(View.VISIBLE);

        ArrayList<TopTv> list = new ArrayList<>();
        list.add(new TopTv(binding.tag1));
        list.add(new TopTv(binding.tag2));
        list.add(new TopTv(binding.tag3));

        int index = getIndex();
        for (int i = 0; i < list.size(); i++) {
            View tag = list.get(i).tag;
            if (i <= index) {
                if (i == index) {
                    tag.setBackgroundResource(R.drawable.select_point);
                    tag.getLayoutParams().width = Utils.dp2Px(this, 24);
                    tag.getLayoutParams().height = Utils.dp2Px(this, 24);
                } else {
                    tag.setBackgroundResource(R.drawable.normal_point);
                    tag.getLayoutParams().width = Utils.dp2Px(this, 12);
                    tag.getLayoutParams().height = Utils.dp2Px(this, 12);
                }
            } else {
                tag.setBackgroundResource(R.drawable.normal_point);
                tag.getLayoutParams().width = Utils.dp2Px(this, 12);
                tag.getLayoutParams().height = Utils.dp2Px(this, 12);
            }
        }
        binding.tv.setText(String.format(getString(R.string.max_amount), Utils.formatNoFloat(tmp.get(2), true)));
        binding.tv.setTextColor(0xAD000000);
    }

    protected void disableAll(View group) {
        if (group instanceof ViewGroup) {
            int j = ((ViewGroup) group).getChildCount();
            for (int i = 0; i < j; i++) {
                View view = ((ViewGroup) group).getChildAt(i);
                if (view instanceof ViewGroup) {
                    disableAll(view);
                } else {
                    view.setEnabled(false);
                }
            }
        } else {
            group.setEnabled(false);
        }
    }

    @Override
    protected void dealBack() {
        if (this instanceof InfoActivity) {
            Report.click("ACTION_II_002");
        } else if (this instanceof WorkActivity) {
            Report.click("ACTION_JJ_002");
        } else if (this instanceof TouchActivity) {
            Report.click("ACTION_KK_002");
        }
        if (fromMe()) {
            finish();
        } else {
            if (fail) {
                finish();
            } else {
                super.dealBack();
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            dealBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    protected String getValue(int id, int i) {
        String[] tmp = getResources().getStringArray(id);
        if (i >= 0 && i < tmp.length) {
            return tmp[i];
        }
        return tmp[0];
    }
}
